<style>
    .xxpayPopupDiv table{
        text-align: center;
    }
    .xxpayPopupDiv .leftT {
        text-align: left;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <div class="layui-tab-content">
                <table id="audit" class="layui-table"></table>
                <form class="layui-form" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">代付通道</label>
                        <div class="layui-input-inline" >
                            <select name="agentpayPassageId" id="agentpayPassageId" lay-filter="agentpayPassageFilter">
                                <option value="">选择代付通道</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">通道子账户</label>
                        <div class="layui-input-inline" >
                            <select name="agentpayPassageAccountId" id="agentpayPassageAccountId" lay-filter="agentpayPassageAccountFilter">
                                <option value="">选择通道子账户</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item" >
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block" style="width:40%">
                            <input type="text" name="remitRemark" id="remTxt" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" >
                            <button id="remitIng" lay-submit lay-filter="remitIng" class="layui-btn layui-btn-warm" data-val="4">打款中</button>
                            <button id="remitTg" lay-submit lay-filter="remitTg" class="layui-btn layui-btn-normal" data-val="5">打款成功</button>
                            <button id="remitNt" lay-submit lay-filter="remitNt" class="layui-btn layui-btn-normal" data-val="7">打款+支付</button>
                            <button id="remitFai" lay-submit lay-filter="remitFai" class="layui-btn layui-btn-danger" data-val="6">打款失败</button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <blockquote class="layui-elem-quote">"打款中","打款成功","打款失败"只会修改结算状态, "打款+支付"会修改结算状态且调代付接口打款</blockquote>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>

    layui.use(['admin', 'table','util','element', 'form'], function(){
        var table = layui.table
        ,$ = layui.$
        ,form = layui.form
        ,admin = layui.admin
        ,view = layui.view
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息
        // var router = layui.router();
        // var creId = router.search.id;
        var creId = layui.view.getOpenParams('id');
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/sett/get',
            data: {
                id: creId,
            },
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                if(res.code == 0 && res.data != null){
                    var html = '';

                    html += `
                        <table>
                            <colgroup>
                                <col width="25%">
                                <col>
                                <col width="25%">
                                <col>
                            </colgroup>
                            <tbody>
                                <tr>
                                    <td>结算订单号</td>
                                    <td class="leftT">${res.data.settOrderId == null ? "" : res.data.settOrderId}</td>
                                    <td>结算日期</td>
                                    <td class="leftT">${ layui.util.toDateString(res.data.settDate, "yyyy-MM-dd") }</td>
                                </tr>
                                <tr>
                                    <td>结算商类型</td>
                                    <td class="leftT">${res.data.infoType==1?"代理商":res.data.infoType==2?"商户":res.data.infoType==3?"":""}</td>
                                    <td>结算商ID</td>
                                    <td class="leftT">${res.data.infoId}</td>
                                </tr>
                                <tr>
                                    <td>结算类型</td>
                                    <td class="leftT">${res.data.settType==1?"手工结算":res.data.settType==2?"自动结算":""}</td>
                                     <td>结算金额</td>
                                    <td class="leftT">${res.data.settAmount/100 }</td>
                                </tr>
                                <tr>
                                    <td>打款金额</td>
                                    <td class="leftT">${res.data.remitAmount/100}</td>
                                    <td>手续费</td>
                                    <td class="leftT">${res.data.settFee/100}</td>
                                </tr>
                                <tr>
                                    <td>账户属性</td>
                                    <td class="leftT">${res.data.accountAttr==0?"对私":res.data.accountAttr==1?"对公":""}</td>
                                    <td>账户类型</td>
                                    <td class="leftT">${res.data.accountType==1?"银行账户":res.data.accountType==2?"微信账户":res.data.accountType==3?"支付账户":""}</td>
                                </tr>
                                <tr>
                                    <td>开户行名称</td>
                                    <td class="leftT">${res.data.bankName}</td>
                                    <td>开户网点名称</td>
                                    <td class="leftT">${res.data.bankNetName}</td>
                                </tr>
                                <tr>
                                    <td>账户名</td>
                                    <td class="leftT">${res.data.accountName}</td>
                                    <td>账户号</td>
                                    <td class="leftT">${res.data.accountNo}</td>
                                </tr>
                                <tr>
                                    <td>开户行所在省</td>
                                    <td class="leftT">${res.data.province}</td>
                                    <td>开户行所在市</td>
                                    <td class="leftT">${res.data.city}</td>
                                </tr>
                                <tr>
                                    <td>结算状态</td>
                                    <td class="leftT">${res.data.settStatus == 1?"等待审核":res.data.settStatus == 2?"已审核":res.data.settStatus == 3?"审核不通过":res.data.settStatus == 4?"打款中":res.data.settStatus == 5?"打款成功":res.data.settStatus == 6?"打款失败":"非法结算状态"}</td>
                                    <td>创建时间</td>
                                    <td class="leftT">${ layui.util.toDateString(res.data.createTime) }</td>
                                </tr>
                                <tr>
                                    <td>操作人ID</td>
                                    <td class="leftT">${res.data.operatorId == null ? "" : res.data.operatorId}</td>
                                    <td>备注</td>
                                    <td class="leftT">${res.data.remark}</td>
                                </tr>
                            </tbody>
                        </table>
                    `
                    $('#audit').html(html);

                    var agentpayPassageId = res.data.agentpayPassageId;
                    // 加载代付通道列表
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/config/common/agentpay_passage_all',
                        error: function(err){
                            layer.alert(err);
                        },
                        success: function(res){
                            if(res.code == 0){
                                var agentpayPassageList = res.data;
                                //遍历赋值
                                for(var i in agentpayPassageList){
                                    if(agentpayPassageList[i].feeType == 1) {
                                        $("#agentpayPassageId").append('<option value= '+agentpayPassageList[i].id+'>'+agentpayPassageList[i].passageName+' [' + agentpayPassageList[i].feeRate + '%]' +'</option>');
                                    }else if(agentpayPassageList[i].feeType == 2) {
                                        $("#agentpayPassageId").append('<option value= '+agentpayPassageList[i].id+'>'+agentpayPassageList[i].passageName+' [' + agentpayPassageList[i].feeEvery/100 + '元/笔]' +'</option>');
                                    }else if(agentpayPassageList[i].feeType == 3) {
                                        $("#agentpayPassageId").append('<option value= '+agentpayPassageList[i].id+'>'+agentpayPassageList[i].passageName+' [' + agentpayPassageList[i].feeRate + '%+' + agentpayPassageList[i].feeEvery/100 + '元/笔]' +'</option>');
                                    }
                                }
                                $("#agentpayPassageId").find('option[value="'+agentpayPassageId+'"]').attr('selected', true);
                                form.render('select');
                            }
                        }
                    });

                    var agentpayPassageAccountId = res.data.agentpayPassageAccountId;
                    // 加载代付通道子账户列表
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/config/common/agentpay_passage_account',
                        data: {agentpayPassageId: agentpayPassageId},
                        error: function(err){
                            layer.alert(err);
                        },
                        success: function(res){
                            if(res.code == 0){
                                var agentpayPassageAccountList = res.data;
                                //遍历赋值
                                for(var i in agentpayPassageAccountList){
                                    $("#agentpayPassageAccountId").append('<option value= '+agentpayPassageAccountList[i].id+'>'+agentpayPassageAccountList[i].accountName+' [' + agentpayPassageAccountList[i].id + ']' +'</option>');
                                }
                                $("#agentpayPassageAccountId").find('option[value="'+agentpayPassageAccountId+'"]').attr('selected', true);
                                form.render('select');
                            }
                        }
                    });
                    // 监控通道列表下拉选择事件
                    form.on('select(agentpayPassageFilter)', function(data){
                        var agentpayPassageId = data.value;
                        // 清除旧的
                        $("#agentpayPassageAccountId option").each(function(){  //遍历所有option
                            var val = $(this).val();   //获取option值
                            if(val != '') {
                                this.remove();
                            }
                        });
                        admin.req({
                            type: 'post',
                            url: layui.setter.baseUrl + '/config/common/agentpay_passage_account',
                            data: {agentpayPassageId: agentpayPassageId},
                            error: function(err){
                                layer.alert(err);
                            },
                            success: function(res){
                                if(res.code == 0){
                                    var agentpayPassageAccountList = res.data;
                                    //遍历赋值
                                    for(var i in agentpayPassageAccountList){
                                        $("#agentpayPassageAccountId").append('<option value= '+agentpayPassageAccountList[i].id+'>'+agentpayPassageAccountList[i].accountName+' [' + agentpayPassageAccountList[i].id + ']' +'</option>');
                                    }
                                    form.render('select');
                                }
                            }
                        });
                    });


                }else {
                    layer.alert('请求数据为空');
                }
            }
        })


        //当点击审核通过按钮时
        form.on('submit(remitIng)', function(data){
            // 在这个回调函数里面写ajax提交
            var curSt = $('#remitIng').data('val');
            layer.confirm("确定修改状态为打款中?", function(index){
                admin.req({
                    type: 'post',
                    url: layui.setter.baseUrl + '/sett/remit',
                    // data: "params=" + JSON.stringify(data.field),
                    data: {
                        remark : data.field.remitRemark,
                        status: curSt,
                        id:creId
                    },
                    error: function(err){
                        layer.alert(err,{title:'发起的请求失败'})
                    },
                    success: function(res){
                        if(res.code == 0){
                            layer.alert(res.msg, function(index){
                                layer.closeAll(); //关闭所有弹层
                                layui.table.reload('tableReload', {page: {curr: 1}}); //调用业务弹层外表格重新加载
                            })
                        }
                    }
                });
            });
            return false;//阻止跳转
        });


        form.on('submit(remitTg)', function(data){
            // 在这个回调函数里面写ajax提交
            var curSt = $('#remitTg').data('val');
            layer.confirm("确定修改状态为打款成功?", function(index){
                admin.req({
                    type: 'post',
                    url: layui.setter.baseUrl + '/sett/remit',
                    data: {
                        remark : data.field.remitRemark,
                        status: curSt,
                        id:creId
                    },
                    error: function(err){
                        layer.alert(err,{title:'发起的请求失败'})
                    },
                    success: function(res){
                        if(res.code == 0){
                            layer.alert(res.msg, function(index){
                                layer.closeAll(); //关闭所有弹层
                                layui.table.reload('tableReload', {page: {curr: 1}}); //调用业务弹层外表格重新加载
                            })
                        }
                    }
                });
            });

            return false;//阻止跳转
        });

        // 打款+支付
        form.on('submit(remitNt)', function(data){
            var agentpayPassageId = $.trim($('#agentpayPassageId').val());
            if(agentpayPassageId == '') {
                layer.alert("请选择代付通道",{title: '提示'});
                return false;
            }

            var agentpayPassageAccountId = $.trim($('#agentpayPassageAccountId').val());
            if(agentpayPassageAccountId == '') {
                layer.alert("请选择代付通道子账户",{title: '提示'});
                return false;
            }

            layer.confirm("确定修改状态为打款成功且执行转账?", function(index){
                layer.msg('正在转账中...', {icon: 16});
                admin.req({
                    type: 'post',
                    url: layui.setter.baseUrl + '/sett/remit2',
                    // data: "params=" + JSON.stringify(data.field),
                    data: {
                        remark : data.field.remitRemark,
                        id:creId,
                        agentpayPassageId: agentpayPassageId,
                        agentpayPassageAccountId : agentpayPassageAccountId
                    },
                    error: function(err){
                        layer.alert(err,{title:'发起的请求失败'})
                    },
                    success: function(res){
                        layer.closeAll('loading');
                        if(res.code == 0){
                            layer.alert(res.msg, function(index){
                                layer.closeAll(); //关闭所有弹层
                                layui.table.reload('tableReload', {page: {curr: 1}}); //调用业务弹层外表格重新加载
                            })
                        }
                    }
                });
            });
            return false;//阻止跳转
        });

        form.on('submit(remitFai)', function(data){
            // 在这个回调函数里面写ajax提交
            var curSt = $('#remitFai').data('val');
            layer.confirm("确定修改状态为打款失败?", function(index){
                admin.req({
                    type: 'post',
                    url: layui.setter.baseUrl + '/sett/remit',
                    // data: "params=" + JSON.stringify(data.field),
                    data: {
                        remark : data.field.remitRemark,
                        status: curSt,
                        id:creId
                    },
                    error: function(err){
                        layer.alert(err,{title:'发起的请求失败'})
                    },
                    success: function(res){
                        if(res.code == 0){
                            layer.alert(res.msg, function(index){
                                layer.closeAll(); //关闭所有弹层
                                layui.table.reload('tableReload', {page: {curr: 1}}); //调用业务弹层外表格重新加载
                            })
                        }
                    }
                });
            });

            return false;//阻止跳转
        });
        form.render();

    })

</script>
